#include "FlashWidget.h"
#include "FlashExport.h"

#include <QtGui/QtGui>

FlashWidget::FlashWidget(QWidget * parent /* = 0 */, Qt::WindowFlags f /* = 0 */):
QWidget(parent, f),m_clipRect(0,0,-1,-1),m_bPaint(true)
{
	m_flash = new QAxWidget(this,0);
	m_flash->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));

	m_flash->setAttribute(Qt::WA_TranslucentBackground, true); 

	connect(m_flash,SIGNAL(FSCommand(QString,QString)),this,SLOT(flashAction(QString,QString)));

	m_flash->hide();
	m_pTimer = new QTimer(this);
	m_pTimer->setInterval(300);

	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(timerOut()));

	m_nCurFrame = 0;
}

FlashWidget::~FlashWidget()
{

}

void FlashWidget::timerOut()
{
	m_flash->dynamicCall("GotoFrame(long)", m_nCurFrame);
	QRect clipRect(0,0,0,0);
	{
		setPaintRect(false);
		update();
		clipRect = getClipRect();

		QImage image(clipRect.width(), clipRect.height(), QImage::Format_ARGB32);

		image.fill(0);
		m_flash->render(&image, QPoint(), QRegion(clipRect));
		//QPixmap pix = QPixmap::grabWidget(m_flash, clipRect);

		QString fileName = m_fileName;
		fileName.remove(m_filePath);
		fileName.remove(".swf");

		image.save(m_filePath + "/" + fileName + QString("_%1").arg(m_nCurFrame) + ".png");
		setPaintRect(true);
	}
	m_nCurFrame++;
	if (m_nCurFrame == m_nFrames)
	{
		m_pTimer->stop();
		m_nCurFrame = 0;
	}
}

void FlashWidget::startAutoSave()
{
	m_pTimer->start();
}

void FlashWidget::mousePressEvent(QMouseEvent *event)
{
	QWidget::mousePressEvent(event);
	m_clipRect.setTopLeft(event->pos());
}

void FlashWidget::mouseMoveEvent(QMouseEvent *event)
{
	QWidget::mouseMoveEvent(event);
	QPoint curPos = event->pos();

	QSize size(curPos.x() - m_clipRect.topLeft().x(), curPos.y() - m_clipRect.topLeft().y());
	m_clipRect.setSize(size);
	update();
}

void FlashWidget::mouseReleaseEvent(QMouseEvent *event)
{
	QWidget::mouseReleaseEvent(event);
	QPoint curPos = event->pos();

	QSize size(curPos.x() - m_clipRect.topLeft().x(), curPos.y() - m_clipRect.topLeft().y());
	m_clipRect.setSize(size);
	update();
}

void FlashWidget::loadFlash(const QString& fileName)
{
	m_flash->dynamicCall("LoadMovie(long,string)", 0, fileName);
	m_flash->dynamicCall("Stop()");
	m_nFrames = m_flash->dynamicCall("TotalFrames()").value<long>();

	long color = m_flash->dynamicCall("BackgroundColor()").value<long>();
	m_flash->dynamicCall("SetWMode(string)", "transparent");
	QString sw = m_flash->dynamicCall("WMode()").toString();
	m_fileName = fileName;
	m_filePath = m_fileName;
	m_filePath.remove(m_filePath.lastIndexOf("\\"), m_filePath.length()-m_filePath.lastIndexOf("\\"));
	update();
}

void FlashWidget::resizeEvent(QResizeEvent *event)
{
	QWidget::resizeEvent(event);
	m_flash->resize(event->size());
}

void FlashWidget::paintEvent( QPaintEvent * event )
{
	QPainter painter(this);

	QPixmap pix = QPixmap::grabWidget(m_flash);

	painter.drawPixmap(rect(), pix);

	if (m_bPaint)
	{
		QPen pen;
		pen.setColor(Qt::green);
		pen.setWidth(2);
		painter.setPen(pen);
		QRect rt(m_clipRect);

		if (rt.width() == -1 || rt.height() == -1)
		{
			painter.drawRect(rect());
		}
		else
		{
			painter.drawRect(m_clipRect);
		}
	}
}

QRect FlashWidget::getClipRect()
{
	if (m_clipRect.width() == -1 || m_clipRect.height() == -1)
	{
		return rect();
	}
	else
	{
		return m_clipRect;
	}
}